package formal.array;

import java.util.Arrays;

/**
 * @author DengYuan2
 * @create 2021-02-03 21:46
 */
public class E_283 {
    public static void main(String[] args) {
        int[] a ={0,1,0,3,12};
        moveZeroes2(a);
        System.out.println(Arrays.toString(a));
    }

    /**
     * 我的写法-特别慢
     * 思路：找到一个，其它的都往前移动；
     * @param nums
     */
    public static void moveZeroes(int[] nums) {
        int count =0;
        for (int i = 0; i < nums.length-count; ) {
            if (nums[i]==0){
                count++;
                for (int j = i+1; j <= nums.length-count; j++) {
                    nums[j-1]=nums[j];
                }
                nums[nums.length-count]=0;
            }else {
                i++;
            }
        }
    }

    /**
     * 大神的写法-牛皮
     * @param nums
     */
    public static void moveZeroes2(int[] nums) {
        int idx=0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]!=0){
                nums[idx++]=nums[i];
            }
        }
        while (idx<nums.length){
            nums[idx++]=0;
        }

    }
}
